package com.cwj.register.controller;

import com.cwj.register.pojo.User;
import com.cwj.register.utils.DBHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet("/saveUser")
public class UserRegister extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] genders = req.getParameterValues("gender");
        String email = req.getParameter("email");
        String[] languages = req.getParameterValues("language[]");
        String intro = req.getParameter("intro");
        System.out.println("获取到的参数有:" + "姓名:" + username + "密码：" + password + "性别:" + genders + "邮箱:" + email + "语言:" + languages + "个人介绍:" + intro);

//        数据封装给pojo类
        User user = new User();

        user.setPassword(password);
        user.setGender(genders);
        user.setEmail(email);
        user.setLanguage(languages);
        user.setIntro(intro);
//        1、获取数据库连接对象
        Connection conn = DBHelper.getConn();
        String gender=null;
        for (String g : genders) {
            gender = g+" ";
        }
        String language=null;
        for (String s : languages) {
            language=s+" ";
        }


        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        2、执行sql语句
        String sql = "INSERT  INTO  user  (username,  password,  gender,  email,  language,  intro)  VALUES  ('"+username
                + "','"
                + password
                + "','"
                + gender
                + "','"
                + email
                + "','"
                + language
                + "','"
                + intro + "')";
        System.out.println(sql);
        try {
            boolean execute = stmt.execute(sql);
            if (execute==false){
                System.out.println("添加用户成功"+username);
                //设置响应格式
                resp.setCharacterEncoding("UTF-8");
                resp.setContentType("text/html;charset=UTF-8");
                req.setAttribute("username",username);
                req.setAttribute("gender",gender);

//                设置注册成功时间
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                String format = simpleDateFormat.format(new Date());

                req.setAttribute("date", format);
                req.getRequestDispatcher("register_success.jsp").forward(req,resp);

            }
            else {
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("注册用户失败!请检查");
            }
        } catch (SQLException e) {
            resp.getWriter().write("请输入合法信息，输入不合理！");
            e.printStackTrace();
        }

//        3、关闭连接
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
